package com.ktmall.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ktmall.pojo.Order;
import com.ktmall.pojo.OrderParam;
import com.ktmall.pojo.Page;
import com.ktmall.service.OrderService;
import com.ktmall.utils.RespMsg;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api(tags = "订单相关接口")
@RestController
@RequestMapping("/uni/member/order")
public class OrderController {
    @Autowired
    private OrderService orderService;

    @ApiOperation("添加订单")
    @PostMapping("/addOrder")
    public RespMsg addOrder(@RequestBody Order order){
        int id = orderService.saveOrder(order);
        Map<String,Integer> map = new HashMap<>();
        map.put("id",id);
        if(id > 0){
            return RespMsg.success("下单成功！",map);
        }else{
            return RespMsg.fail("下单失败！");
        }
    }

    @ApiOperation("根据用户选择查询订单详情")
    @GetMapping("/findById")
    public RespMsg findById(Integer id){
        Order order = orderService.findById(id);
        return RespMsg.success("订单详情查询成功",order);
    }

    @ApiOperation("分页查询订单列表")
    @PostMapping("/pageOrderList")
    public RespMsg pageOrderList(@RequestBody Map<String,Integer> orderParam){
        int count = orderService.getCount( orderParam.get("userId"),  orderParam.get("orderState"));
        List<Order> list = orderService.getOrderListPage( orderParam.get("currPageNo"),  orderParam.get("pageSize"), orderParam.get("userId"), orderParam.get("orderState"));

        Page page = new Page();
        page.setPageSize( orderParam.get("pageSize"));
        page.setTotalCount(count);
        page.setCurrPageNo( orderParam.get("currPageNo"));
        page.setList(list);
        return RespMsg.success("订单列表加载成功！",page);
    }

    @PostMapping("/buyAgain")
    public RespMsg buyAgain(@RequestBody Map<String,Integer> map){
        return  RespMsg.success("再次购买",orderService.buyAgain(map.get("userId"),map.get("orderId")));
    }

    @GetMapping("/takeGoods")
    public RespMsg takeGoods(Integer id){
        int i = orderService.takeGoods(id);
        if(i>0){
            return RespMsg.success("收货成功");
        }else{
            return RespMsg.fail("收货失败");
        }
    }

    @GetMapping("/review")
    public RespMsg review(Integer id){
        int i = orderService.review(id);
        if(i>0){
            return RespMsg.success("评价成功");
        }else{
            return RespMsg.fail("评价失败");
        }
    }
}
